import 'package:async/async.dart';
import 'package:boss/main.dart';
import 'package:boss/models/order_detail_data.dart';
import 'package:boss/ui/page/reportForm/order/page/orderDetail/model/package_group_cart.dart';
import 'package:flutter/material.dart';

class OrderDetailProvider extends ChangeNotifier {
  OrderDetailData data;
  String unionOrderId;

  List<PackageGroupCart> get carts {
    List<PackageGroupCart> temp = [];

    for (final item in data.orderDetailList) {
      if (item.commodityType != 3 || (item.commodityType == 3 && item.porderId == 0)) {
        temp.add(PackageGroupCart(item, []));
      } else {
        temp.last.children.add(item);
      }
    }

    return temp;
  }

  OrderDetailProvider({this.unionOrderId});

  Future<Result<OrderDetailData>> queryOrderDetail() async {
    try {
      var response = await api.queryOrderDetailByUnionOrderId(unionOrderId);
      if (!response.successful()) {
        return Result.error(response.msg);
      }

      data = response.data;
      notifyListeners();

      return Result.value(response.data);
    } catch (e) {
      return Result.error(e.toString());
    }
  }
}
